package com.desaysv.svhttp.utils;

import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

/**
 * @author linzi
 * @version 1.0
 * @date 2022-7-19
 * @since 2022-7-19
 */
public class SVUtils {

    /**
     * 将输入流转换成字符串
     *
     * @param is 从网络获取的输入流
     * @return byte[]
     */
    public static byte[] streamTobyte(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            baos.close();
            is.close();
            return baos.toByteArray();
        } catch (Exception e) {
            Log.e("HttpManager", e.toString());
            return null;
        }
    }

    /**
     * @param plainText 需要MD5加密的字符串
     * @return MD5加密后的字符串
     */
    public static String getMd5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte[] b = md.digest();
            int i;

            StringBuilder buf = new StringBuilder();
            for (byte value : b) {
                i = value;
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            //32位加密
            return buf.toString();
            // 16位的加密
            //return buf.toString().substring(8, 24);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 小写字母集
     */
    private static final String[] LOWER_CASE = {
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    /**
     * 大写字母集
     */
    private static final String[] CAPITAL = {
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /**
     * 数字集
     */
    private static final String[] NUMBER = {
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    /**
     * 特殊字符集
     */
    private static final String[] SIGN = {
            "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "`", "-", "=",
            "{", "}", "|", ":", "\"", "<", ">", "?",
            "[", "]", "\\", ";", "'", ",", ".", "/"};
    /**
     * 静态随机数
     */
    private static final Random RANDOM = new Random();

    /**
     * 获取随机组合码
     *
     * @param num  位数
     * @param type 类型
     *             <br>字符型 LETTER,
     *             <br>大写字符型 CAPITAL,
     *             <br>数字型 NUMBER,
     *             <br>符号型 SIGN,
     *             <br>大+小字符型 LETTER_CAPITAL,
     *             <br>小字符+数字 型 LETTER_NUMBER,
     *             <br>大+小字符+数字 型 LETTER_CAPITAL_NUMBER,
     *             <br>大+小字符+数字+符号 型 LETTER_CAPITAL_NUMBER_SIGN
     * @return 随即字符串
     */
    public static String getRandom(int num, TYPE type) {
        ArrayList<String> temp = new ArrayList<>();
        if (type == TYPE.LETTER) {
            temp.addAll(Arrays.asList(LOWER_CASE));
        } else if (type == TYPE.CAPITAL) {
            temp.addAll(Arrays.asList(CAPITAL));
        } else if (type == TYPE.NUMBER) {
            temp.addAll(Arrays.asList(NUMBER));
        } else if (type == TYPE.SIGN) {
            temp.addAll(Arrays.asList(SIGN));
        } else if (type == TYPE.LETTER_CAPITAL) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(CAPITAL));
        } else if (type == TYPE.LETTER_NUMBER) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(NUMBER));
        } else if (type == TYPE.LETTER_CAPITAL_NUMBER) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(CAPITAL));
            temp.addAll(Arrays.asList(NUMBER));
        } else if (type == TYPE.LETTER_CAPITAL_NUMBER_SIGN) {
            temp.addAll(Arrays.asList(LOWER_CASE));
            temp.addAll(Arrays.asList(CAPITAL));
            temp.addAll(Arrays.asList(NUMBER));
            temp.addAll(Arrays.asList(SIGN));
        }
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < num; i++) {
            code.append(temp.get(RANDOM.nextInt(temp.size())));
        }
        return code.toString();
    }

    /**
     * 密码类型枚举
     *
     * @author ASUS
     */
    public enum TYPE {
        /**
         * 字符型
         */
        LETTER,
        /**
         * 大写字符型
         */
        CAPITAL,
        /**
         * 数字型
         */
        NUMBER,
        /**
         * 符号型
         */
        SIGN,
        /**
         * 大+小字符 型
         */
        LETTER_CAPITAL,
        /**
         * 小字符+数字 型
         */
        LETTER_NUMBER,
        /**
         * 大+小字符+数字 型
         */
        LETTER_CAPITAL_NUMBER,
        /**
         * 大+小字符+数字+符号 型
         */
        LETTER_CAPITAL_NUMBER_SIGN
    }

}
